home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / flip / vect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.5 KB  |  216 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * vect -
  19.  *    Various functions to support operations on vectors.
  20.  *
  21.  * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
  22.  *
  23.  * Modified for my own nefarious purposes-- Gavin Bell
  24.  */
  25. #include "vect.h"
  26.  
  27. float *
  28. vnew()
  29. {
  30.     register float *v;
  31.  
  32.     v = (float *) malloc(sizeof(float)*3);
  33.     return v;
  34. }
  35.  
  36. float *
  37. vclone(v)
  38. float *v;
  39. {
  40.     register float *c;
  41.  
  42.     c = vnew();
  43.     vcopy(v, c);
  44.     return c;
  45. }
  46.  
  47. void
  48. vcopy(v1,v2)
  49. float *v1, *v2;
  50. {
  51.     register int i;
  52.     for (i = 0 ; i < 3 ; i++)
  53.         v2[i] = v1[i];
  54. }
  55.  
  56. void
  57. vprint(v)
  58. float *v;
  59. {
  60.     printf("x: %f y: %f z: %f\n",v[0],v[1],v[2]);
  61. }
  62.  
  63. void
  64. vset(v,x,y,z)
  65. float *v;
  66. float x, y, z;
  67. {
  68.     v[0] = x;
  69.     v[1] = y;
  70.     v[2] = z;
  71. }
  72.  
  73. void
  74. vzero(v)
  75. float *v;
  76. {
  77.     v[0] = 0.0;
  78.     v[1] = 0.0;
  79.     v[2] = 0.0;
  80. }
  81.  
  82. void
  83. vnormal(v)
  84. float *v;
  85. {
  86.     vscale(v,1.0/vlength(v));
  87. }
  88.  
  89. float
  90. vlength(v)
  91. float *v;
  92. {
  93.     return fsqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  94. }
  95.  
  96. void
  97. vscale(v,div)
  98. float *v;
  99. float div;
  100. {
  101.     v[0] *= div;
  102.     v[1] *= div;
  103.     v[2] *= div;
  104. }
  105.  
  106. void
  107. vmult(src1,src2,dst)
  108. float *src1, *src2, *dst;
  109. {
  110.     dst[0] = src1[0] * src2[0];
  111.     dst[1] = src1[1] * src2[1];
  112.     dst[2] = src1[2] * src2[2];
  113. }
  114.  
  115. void
  116. vadd(src1,src2,dst)
  117. float *src1, *src2, *dst;
  118. {
  119.     dst[0] = src1[0] + src2[0];
  120.     dst[1] = src1[1] + src2[1];
  121.     dst[2] = src1[2] + src2[2];
  122. }
  123.  
  124. void
  125. vsub(src1,src2,dst)
  126. float *src1, *src2, *dst;
  127. {
  128.     dst[0] = src1[0] - src2[0];
  129.     dst[1] = src1[1] - src2[1];
  130.     dst[2] = src1[2] - src2[2];
  131. }
  132.  
  133. void
  134. vhalf(v1,v2,half)
  135. float *v1, *v2, *half;
  136. {
  137.     float len;
  138.  
  139.     vadd(v2,v1,half);
  140.     len = vlength(half);
  141.     if(len>0.0001)
  142.         vscale(half,1.0/len);
  143.     else
  144.         *half = *v1;
  145. }
  146.  
  147. float
  148. vdot(v1,v2)
  149. float *v1, *v2;
  150. {
  151.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  152. }
  153.  
  154. void
  155. vcross(v1, v2, cross)
  156. float *v1, *v2, *cross;
  157. {
  158.     float temp[3];
  159.  
  160.     temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  161.     temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  162.     temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  163.     vcopy(temp, cross);
  164. }
  165.  
  166. void
  167. vdirection(v1, dir)
  168. float *v1, *dir;
  169. {
  170.     *dir = *v1;
  171.     vnormal(dir);
  172. }
  173.  
  174. void
  175. vreflect(in,mirror,out)
  176. float *in, *mirror, *out;
  177. {
  178.     float temp[3];
  179.  
  180.     vcopy(mirror, temp);
  181.     vscale(temp,vdot(mirror,in));
  182.     vsub(temp,in,out);
  183.     vadd(temp,out,out);
  184. }
  185.  
  186. void
  187. vmultmatrix(m1,m2,prod)
  188. float m1[4][4], m2[4][4], prod[4][4];
  189. {
  190.     register int row, col;
  191.     float temp[4][4];
  192.  
  193.     for(row=0 ; row<4 ; row++) 
  194.         for(col=0 ; col<4 ; col++)
  195.             temp[row][col] = m1[row][0] * m2[0][col]
  196.                            + m1[row][1] * m2[1][col]
  197.                            + m1[row][2] * m2[2][col]
  198.                            + m1[row][3] * m2[3][col];
  199.     for(row=0 ; row<4 ; row++) 
  200.         for(col=0 ; col<4 ; col++)
  201.         prod[row][col] = temp[row][col];
  202. }
  203.  
  204. vtransform(v,mat,vt)
  205. float *v;
  206. float mat[4][4];
  207. float *vt;
  208. {
  209.     float t[3];
  210.  
  211.     t[0] = v[0]*mat[0][0] + v[1]*mat[1][0] + v[2]*mat[2][0] + mat[3][0];
  212.     t[1] = v[0]*mat[0][1] + v[1]*mat[1][1] + v[2]*mat[2][1] + mat[3][1];
  213.     t[2] = v[0]*mat[0][2] + v[1]*mat[1][2] + v[2]*mat[2][2] + mat[3][2];
  214.     vcopy(t, vt);
  215. }
  216.